View Javadoc

1   /*
2   	Copyright 2008-2014 ITACA-TSB, http://www.tsb.upv.es
3   	Instituto Tecnologico de Aplicaciones de Comunicacion 
4   	Avanzadas - Grupo Tecnologias para la Salud y el 
5   	Bienestar (TSB)
6   	
7   	See the NOTICE file distributed with this work for additional 
8   	information regarding copyright ownership
9   	
10  	Licensed under the Apache License, Version 2.0 (the "License");
11  	you may not use this file except in compliance with the License.
12  	You may obtain a copy of the License at
13  	
14  	  http://www.apache.org/licenses/LICENSE-2.0
15  	
16  	Unless required by applicable law or agreed to in writing, software
17  	distributed under the License is distributed on an "AS IS" BASIS,
18  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  	See the License for the specific language governing permissions and
20  	limitations under the License.
21   */
22  package org.universAAL.samples.util.mini;
23  
24  import org.osgi.framework.BundleActivator;
25  import org.osgi.framework.BundleContext;
26  import org.universAAL.middleware.container.ModuleContext;
27  import org.universAAL.middleware.container.osgi.uAALBundleContainer;
28  import org.universAAL.middleware.context.ContextEvent;
29  import org.universAAL.middleware.context.ContextEventPattern;
30  import org.universAAL.middleware.service.CallStatus;
31  import org.universAAL.middleware.service.ServiceCall;
32  import org.universAAL.middleware.service.ServiceResponse;
33  import org.universAAL.middleware.ui.UIResponse;
34  import org.universAAL.middleware.util.Constants;
35  import org.universAAL.ontology.location.Location;
36  import org.universAAL.ontology.profile.User;
37  import org.universAAL.ontology.profile.service.ProfilingService;
38  import org.universAAL.support.utils.ICListener;
39  import org.universAAL.support.utils.ISListener;
40  import org.universAAL.support.utils.IUIListener;
41  import org.universAAL.support.utils.UAAL;
42  import org.universAAL.support.utils.context.Pattern;
43  import org.universAAL.support.utils.service.Path;
44  import org.universAAL.support.utils.service.mid.UtilEditor;
45  import org.universAAL.support.utils.ui.Forms;
46  import org.universAAL.support.utils.ui.low.Dialog;
47  
48  public class Activator implements BundleActivator {
49      // OSGi & uAAL contexts
50      public static BundleContext osgiContext = null;
51      public static ModuleContext context = null;
52      // Namespace constant to reuse
53      private static String NAMESPACE = "http://ontology.itaca.upv.es/Test.owl#";
54      // The UAAL helper
55      private UAAL u;
56  
57      public void start(BundleContext bcontext) throws Exception {
58  	// Get the uAAL module context
59  	Activator.osgiContext = bcontext;
60  	Activator.context = uAALBundleContainer.THE_CONTAINER
61  		.registerModule(new Object[] { bcontext });
62  
63  	// Instantiate the UAAL helper
64  	u = new UAAL(context);
65  	
66  	// Subscribe events [Subject: User, Predicate: hasLocation]
67  	Pattern cep = new Pattern(User.MY_URI, User.PROP_PHYSICAL_LOCATION, null);
68  	u.subscribeC(new ContextEventPattern[] { cep }, new ICListener() {
69  	    public void handleContextEvent(ContextEvent event) {
70  		System.out.println(">>> Received Event: " + event.toString());
71  	    }
72  	});
73  	
74  	// Send event [Subject: user1, Predicate: hasLocation, Object: loc1]
75  	User user1 = new User(Constants.uAAL_MIDDLEWARE_LOCAL_ID_PREFIX + "saied");
76  	user1.setLocation(new Location(NAMESPACE + "loc1"));
77  	ContextEvent e = new ContextEvent(user1, User.PROP_PHYSICAL_LOCATION);
78  	u.sendC(e);
79  			
80  	// Provide service [Profiling services GET/SET/ADD/REMOVE a User]
81  	u.provideS(UtilEditor.getServiceProfiles(NAMESPACE,
82  		ProfilingService.MY_URI,
83  		Path.at(ProfilingService.PROP_CONTROLS).path, User.MY_URI),
84  		new ISListener() {
85  		    public ServiceResponse handleCall(ServiceCall s) {
86  			System.out.println(">>> Received Service Call: " + s.toString());
87  			return new ServiceResponse(CallStatus.succeeded);
88  		    }
89  		});
90  
91  	// Call service [Profiling service REMOVE user1]
92  	ServiceResponse r = u.callS(UtilEditor.requestRemove(
93  		ProfilingService.MY_URI,
94  		Path.at(ProfilingService.PROP_CONTROLS).path, user1));
95  	System.out.println(">>> Received Service Response: "+ r.getCallStatus());
96  	
97  	// Request UI [Output: "Successfully reached UI test", Submit:"OK"]
98  	Dialog d = new Dialog(user1,"UI example");
99  	d.add(Forms.out("Result:", "Successfully reached UI test"));
100 	d.addSubmit(Forms.submit(NAMESPACE + "button1", "OK"));
101 	u.requestUI(d, new IUIListener() {
102 	    public void handleUIResponse(UIResponse r) {
103 		System.out.println(">>> Received UI Response: "	+ r.getSubmissionID());
104 	    }
105 	});
106 
107     }
108 
109     public void stop(BundleContext arg0) throws Exception {
110 	// Close uAAL wrappers and free resources
111 	u.terminate();
112     }
113 
114 }